package com.mdd.service.divide.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayTradeOrderSettleRequest;
import com.alipay.api.request.AlipayTradeRoyaltyRateQueryRequest;
import com.alipay.api.request.AlipayTradeRoyaltyRelationBindRequest;
import com.alipay.api.request.AlipayTradeRoyaltyRelationUnbindRequest;
import com.alipay.api.response.*;
import com.mdd.service.divide.DivideAccountService;
import org.springframework.stereotype.Service;

/**
 * 分账
 *
 * @Description
 * @Author 幻速
 * @Date 2023/11/10 15:37
 **/
@Service
public class DivideAccountServiceImpl implements DivideAccountService {
    /**
     * 绑定联系人信息
     *
     * @param alipayClient 阿里请求对象
     * @param model        绑定信息人
     * @return 结果
     * @throws AlipayApiException 请求异常
     */
    @Override
    public AlipayTradeRoyaltyRelationBindResponse royaltyRelationBind(AlipayClient alipayClient, AlipayTradeRoyaltyRelationBindModel model) throws AlipayApiException {
        AlipayTradeRoyaltyRelationBindRequest request = new AlipayTradeRoyaltyRelationBindRequest();
        request.setBizModel(model);
        return alipayClient.certificateExecute(request);
    }

    /**
     * @param alipayClient 阿里请求对象
     * @param model        解绑人信息
     * @return 结果
     * @throws AlipayApiException 请求异常
     */
    @Override
    public AlipayTradeRoyaltyRelationUnbindResponse royaltyRelationUnbind(AlipayClient alipayClient, AlipayTradeRoyaltyRelationUnbindModel model) throws AlipayApiException {
        AlipayTradeRoyaltyRelationUnbindRequest request = new AlipayTradeRoyaltyRelationUnbindRequest();
        request.setBizModel(model);
        return alipayClient.certificateExecute(request);
    }

    /**
     * 分账结算
     *
     * @param alipayClient 阿里请求对象
     * @param model        分账信息
     * @return 结果
     * @throws AlipayApiException 请求异常
     */
    @Override
    public AlipayTradeOrderSettleResponse tradeOrderSettle(AlipayClient alipayClient, AlipayTradeOrderSettleModel model) throws AlipayApiException {
        AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();
        request.setBizModel(model);
        return alipayClient.certificateExecute(request);
    }

    /**
     * 分账查询
     * @param alipayClient 阿里请求对象
     * @param model 分账信息
     * @return 结果
     * @throws AlipayApiException 请求异常
     */
    @Override
    public AlipayTradeRoyaltyRateQueryResponse royaltyRateQuery(AlipayClient alipayClient, AlipayTradeRoyaltyRateQueryModel model) throws AlipayApiException {
        AlipayTradeRoyaltyRateQueryRequest request=new AlipayTradeRoyaltyRateQueryRequest();
        request.setBizModel(model);
        return alipayClient.certificateExecute(request);
    }

    /**
     * 分账结算
     * @param alipayClient 阿里请求对象
     * @param tradeNo 订单
     * @return 结果
     * @throws AlipayApiException 请求异常
     */
    @Override
    public AlipayTradeOrderSettleResponse orderOnSettleQuery(AlipayClient alipayClient, String tradeNo) throws AlipayApiException {
        AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("trade_no",tradeNo);
        request.setBizContent(jsonObject.toJSONString());
        return alipayClient.certificateExecute(request);
    }
}
